package MathTextModeler
{
	import MathText.WordProblem;
	import MathText.WordProblemDisplay;
	
	import MyDisplayObjects.DraggableImage;
	import MyDisplayObjects.DraggableShape;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	/** This is the highest level object for the model display portion of the tool.  
	 * This object allows a user to manipulate objects to model the given word problem */
	public class ModelDisplayContainer extends MovieClip
	{
		// private vars, inaccessible outside of class
		private var wordProblemDisplay:WordProblemDisplay;
		private var problemIntro:ProblemIntro;
		private var prevInitialToolDisplay:String="";
		private var sentenceModeler:SentenceModeler;
		private var actionModeler:ActionModeler;
		private var diagramDisplay:DiagramDisplay;
		private var sentenceCount:uint = 0; // number of sentences we have modelled (initially);
		private var optionsXML:XML, imagesXML:XML, errorsXML:XML;
		private var hidePeripheralText:Boolean, modelSentencesInSerial:Boolean;
		
		public function ModelDisplayContainer(width:Number, height:Number, wordProblemDisplay:WordProblemDisplay, optionsXML:XML, imagesXML:XML, errorsXML:XML)
		{
			this.optionsXML = optionsXML;
			this.imagesXML = imagesXML;
			this.errorsXML = errorsXML;
			this.graphics.beginFill(0xEEEEFF, 1);
			this.graphics.drawRect(0, 0, width, height);
			this.graphics.endFill();			
			this.wordProblemDisplay = wordProblemDisplay;
			// get sentence modeling options
			if (optionsXML.child("sentence").(attribute("hidePeripheralText") == "true").length() > 0) { hidePeripheralText = true; } else { hidePeripheralText = false; }
			if (optionsXML.child("sentence").(attribute("modelSentencesInSerial")=="true").length() > 0){ modelSentencesInSerial = true; } else { modelSentencesInSerial = false; }
			showIntro();
			//example();
		}
		
		/** Show the problem display intro, when completed move on */
		private function showIntro():void
		{
			problemIntro = new ProblemIntro (this.width, this.height, "Please read the problem carefully before beginning.");
			problemIntro.addEventListener( Event.COMPLETE, handleProblemIntroComplete);
			this.addChild(problemIntro);
		}
		
		/** Remove problem intro, move onto sentence modelling */
		private function handleProblemIntroComplete (event:Event):void
		{
			problemIntro.removeEventListener( Event.COMPLETE, handleProblemIntroComplete);
			this.removeChild(problemIntro);
			
			// model sentences
			sentenceModeler = new SentenceModeler(this.width, this.height, wordProblemDisplay, sentenceCount, optionsXML, imagesXML, errorsXML, prevInitialToolDisplay);
			sentenceModeler.addEventListener(Event.COMPLETE, displayActionModeler);
			this.addChild(sentenceModeler);
		}
		
		
		/** Displays a sentence panel that shows all three (or whatever) sentences adjacent to each other so that objects can be moved around */ 
		public function displayActionModeler(event:Event):void
		{
			sentenceModeler.removeEventListener( Event.COMPLETE, displayActionModeler);
			this.diagramDisplay = DiagramDisplay(sentenceModeler.diagramDisplay);
			this.removeChild(sentenceModeler);
			wordProblemDisplay.highlightSubstring("");
			if (actionModeler == null){
				actionModeler = new ActionModeler(this.diagramDisplay, sentenceModeler.toolDisplay, wordProblemDisplay.wordProblem, optionsXML, errorsXML);
				this.addChild(actionModeler);	
			} else {
				this.addChild(actionModeler);
				actionModeler.restore();
			}
			actionModeler.addEventListener(Event.COMPLETE, handleActionModelerComplete);
			actionModeler.addEventListener(Event.CANCEL, handleActionModelerCancel);
		}
		
		/** This function is called when the user decides to edit a sentence */
		private function handleActionModelerCancel (event:Event):void
		{
			actionModeler.removeEventListener(Event.COMPLETE, handleActionModelerComplete);
			actionModeler.removeEventListener(Event.CANCEL, handleActionModelerCancel);
			this.removeChild(actionModeler);
			sentenceModeler.addEventListener( Event.COMPLETE, displayActionModeler);
			this.addChild(sentenceModeler);
			sentenceModeler.restore();
		}
		
		/** This function is called when the user has completed the modelling of the actions in this panel */
		private function handleActionModelerComplete(event:Event):void
		{
			actionModeler.removeEventListener(Event.COMPLETE, handleActionModelerComplete);
			actionModeler.removeEventListener(Event.CANCEL, handleActionModelerCancel);
			this.dispatchEvent(new Event(Event.COMPLETE));
		}			
	}
}